#!/usr/bin/env python2.4
__license__ = "GNU Lesser General Public License, Ver.2"
__author__ = "Grigory Bakunov <bobuk@justos.org>"

import web
import os
import form
import markdown

from config import *
from wmauth import *
from wmconfig import *
from wmforms import *

import wmtools


###
# ADMIN PART
###

class admin:
    def GET(self):
        (user, edit) = getAccess(web.cookies())
        frm = adminIndexForm()
        web.render('admin.html')
        
    def POST(self):
         (user, edit) = getAccess(web.cookies())
         frm = adminIndexForm()
         inp = web.input()
         if not frm.validates(inp) or \
            not User(inp.login).checkpass(inp.password):
             return web.render('admin.html')
         ck = web.cookies()
         web.setcookie('wmUser', inp.login)
         web.setcookie('wmSeq', User(inp.login).getseq(inp.password))
         return web.redirect(Config().cfg['c_abspath'] + '/admin')  

class logout:
    def GET(self):
        ck = web.cookies()
        web.setcookie('wmUser', '', -1)
        web.setcookie('wmSeq', '', -1)
        return web.redirect(Config().cfg['c_abspath'])  

class config:
    def GET(self):
        (user, edit) = getAccess(web.cookies())
        if edit:
            frm = formConfig()
            web.render('config.html')
        else:
            wmtools.raiseError(2)

    def POST(self):
        (user, edit) = getAccess(web.cookies())
        if edit:
            out = []
            cfg = wmconfig.Config()
            inp = web.input()
            cfg.update(inp)
            cfg.save()
            return web.redirect(Config().cfg['c_abspath'] + '/admin/config')
        else:
            wmtools.raiseError(2)


class users:
    def GET(self):
        (user, edit) = getAccess(web.cookies())
        if not edit:
            wmtools.raiseError(2)
        userlist = getUsersList()
        web.render('users.html')

class userDel:
    def GET(self, username):
        (user, edit) = getAccess(web.cookies())
        userlist = getUsersList()
        if not edit or not (username in userlist):
            wmtools.raiseError(2)
        ur = User(username)
        ur.delete()
        return web.redirect(Config().cfg['c_abspath'] + '/admin/users')


class userChange:
    def POST(self,username):
        (user, edit) = getAccess(web.cookies())
        userlist = getUsersList()
        if not edit or not (username in userlist):
            wmtools.raiseError(2)
        inp = web.input()
        if inp.pass1 != inp.pass2:
            return wmtools.raiseError(0)
        ur = User(username)
        ur.info = inp.info.split('\n')
        if len(inp.pass1.strip()) != 0:
            ur.setpass(inp.pass1)
        ur.save()
        return web.redirect(Config().cfg['c_abspath'] + '/admin/users')

class userNew:
    def POST(self):
        (user, edit) = getAccess(web.cookies())
        userlist = getUsersList()
        if not edit:
            wmtools.raiseError(2)
        inp = web.input()
        if inp.pass1 != inp.pass2:
            return wmtools.raiseError(0)
        if inp.login in userlist:
            return wmtools.raiseError(1)
        ur = User(inp.login)
        ur.info = inp.info.split('\n')
        if len(inp.pass1.strip()) != 0:
            ur.setpass(inp.pass1)
        ur.save()
        return web.redirect(Config().cfg['c_abspath'] + '/admin/users')

class textList:
    def GET(self):
        (user, edit) = getAccess(web.cookies())
        # Little hack with short explanation:
        # os.listdir return list of files in creation order.
        # but I want to see newest files first. So i reverse it :)
        pages = [ x[:-4] for x in os.listdir(pagesPath) \
                       if len(x) > 4 and x[-4:] == '.mdn']
        pages.reverse()
        web.render('text.list.html')

    def POST(self):
        (user, edit) = getAccess(web.cookies())
        if not edit:
            wmtools.raiseError(2)
        inp = web.input()
        pagename = inp.pagename
        _fl = open(pagesPath + pagename + '.mdn','w'); _fl.write(inp.etext); _fl.close()
        _fl = open(pagesPath + pagename + '.html','w'); _fl.write(markdown.markdown(inp.etext)); _fl.close()
        web.redirect(Config().cfg['c_abspath'] + '/txt.' + pagename)
